<?php

namespace Management\Controller;

use Think\Controller;

use Myself\FileSystem;

class BuyersController extends BaseController {



	protected $m = NULL;

	protected $m_class = NULL;

	protected $m_config = NULL;

    // protected $statusArr=array("未认证","已认证");
    protected $is_status=array(
    	['color'=>'default','status'=>"未处理"],
    	['color'=>'dark','status'=>"已处理"],
    );
    protected $statusArr=array(

//        0.未付款 1. 未发货 2. 发货中 3. 已完成 4. 订单完成 5. 超时取消

        ['color'=>'dark','status'=>"未付款"],
        ['color'=>'default','status'=>"待分配"],
        ['color'=>'default','status'=>"已分配"],
        ['color'=>'blue','status'=>"司机到达"],
        ['color'=>'green','status'=>"待评价"],
        ['color'=>'dark','status'=>"评价完成"],
        ['color'=>'red','status'=>"已退款"],
        ['color'=>'default','status'=>"已取消"],

    );
	

	public function _initialize(){

		parent::_initialize();

		$this->m = M('order_valuation');

		

	}

		

	

	

    public function index(){

		header('Content-Type:text/html;charset=utf-8');

		$where=[];

		//-----------------搜索-------------------
		I('key') ? $where['tel'] = array('like','%'.I('key').'%') : false;


		//-------------------------搜索end-----------------

		$pageno = I('p',1,'int');

		$pageCount = 10;

		$list = $this->m->where($where)->page($pageno.','.$pageCount)->select();
		if(!empty($list)){
			foreach ($list as $k => $v) {
				$list[$k]['nickname'] = M('member')->where(['id'=>$v['member_id']])->getField('nickname');
				if(!empty($v['title_time'])){
					$title_time = strtotime($v['title_time']);
					if($title_time-86400<time()){
						//录入时间的前一天 超过现在时间
						$list[$k]['is_date']=1;
					}
				}
			}
		}
		// ll($list);die;
		$this->assign('list',$list);// 赋值数据集

		$count = $this->m->where($where)->count();// 查询满足要求的总记录数

		$this->assign('count',$count);// 赋值分页输出	

		$Page = new \Think\Page($count,$pageCount);// 实例化分页类 传入总记录数和每页显示的记录数

		$show = $Page->showManage();// 分页显示输出

		$this->assign('page',$show);// 赋值分页输出

        //-------------------------固定参数输出-----------------

        $this->assign('statusArr',$this->statusArr);// 状态
        $this->assign('is_status',$this->is_status);// 处理状态

		$this->display('index');

    }
    //意向记录详情
	public function edit(){

		$id = I('id',0,'int');

		$order = $this->m->find($id);

		$order['receipt'] = json_decode($order['receipt']);

		$order['relay'] = json_decode($order['relay']);

		$order['delivery_address'] = json_decode($order['delivery_address']);

		$member = M('member')->find($order['member_id']);

		$order['member'] = $member['username'];

		$detail = M('order_detail')->where('order_no ="'.$order['order_no'].'"')->select();

		foreach($detail as $k=>$v){

			$detail[$k]['attribute_cn'] = json_decode($detail[$k]['attribute_cn']);

		}
		$floor_cost = M('product')->where(['id'=>I('product_id')])->getField('floor_cost');
		// ll(json_decode($floor_cost),true);die;
        $this->assign('floor_cost',json_decode($floor_cost,true));//选择楼层
        // ll(M('product')->select());die;
        //车型
        $this->assign('cartypeList',M('cartype')->order('id asc')->select());
        //当前默认车型
        $this->assign('cartypeInfo',M('cartype')->order('id asc')->find());
		$order['detail'] = $detail;
		$this->assign('order',$order);
		$this->assign('startpoint',$order['startpoint']);
		$this->assign('endpoint',$order['endpoint']);
		$this->assign('back_url',$_SERVER['HTTP_REFERER']);
        $this->assign('statusArr',$this->statusArr);// 状态
        $this->assign('statusdetail',$this->statusdetail);// 状态
        $this->assign('lang',I('get.lang','cn'));
        $this->assign('driverList',M('driver')->where(['cartype_id'=>json_decode($order['cartype_info'])->cartype_id])->select());
        //搬家类型楼层
        $this->assign('productList',M('product')->select());
        //车型
        $this->assign('cartypeList',M('cartype')->select());
        //额外费用添加
        $attachment = M('attachment')->where('type!=4 and type!=5 and product_id='.I('product_id'))->select();
        if(!empty($attachment)){
        	foreach ($attachment as $k=> $v) {
        		$child = M('attachment_option')->where(['attachment_id'=>$v['id']])->select();
        		if(!empty($child)){
        			foreach ($child as $a => $b) {
        				$child[$a]['num']=0;
        			}
        		}
        		$attachment[$k]['child'] = $child;
        	}
        }
        // ll($attachment);die;
        $this->assign('attachmentList',$attachment);
		$this->display('edit');
    }
    //weapon意向下单
	public function save(){
		$data = I('post.');
		// ll($data);die;
		$attachment =[];
		if(!empty($data['attachment'])){
			$atta = json_decode(htmlspecialchars_decode($data['attachment']),true);
			if(!empty($atta)){
				foreach ($atta as $k => $v) {
					if(!empty($v['child'])){
						foreach ($v['child'] as $a => $b) {
							// ll($b);
							if($b['num']>0){
								array_push($attachment,$b);
							}
						}
					}
				}
			}
		}
		// ll($attachment);
		$attach_price =0;
		if(!empty($attachment)){
			foreach ($attachment as $key=>$value){
			 	$attachment[$key]['attachment_title']=M('attachment')->where(['id'=>$value['attachment_id']])->getField('title');
			 	$attachment[$key]['price']=$value['num']*$value['value'];
			 	$attachment[$key]['option_id']=$value['attachment_id'];
	            $res=M('attachment_option')->find($value->id);
	            $attach_price+=$attachment[$key]['price'];
	        }
	    }
        $arr['attachment']=json_encode(['list'=>$attachment,'price'=>$attach_price]);
	    // ll($data);die;
		$productInfo=M('product')->where(['id'=>$data['product_id']])->find();
		$cartypeInfo=M('cartype')->where(['id'=>$data['cartype_id']])->find();
		$floorInfo=json_decode($productInfo['floor_cost'],true);
		if($data['product_id']==1){
			$flag_fall_price = $cartypeInfo['flag_fall_price'];//起步价
			$over_charge = $cartypeInfo['over_charge'];//超公里
		}elseif($data['product_id']==2){
			$flag_fall_price = $cartypeInfo['out_price'];//起步价
			$over_charge = $cartypeInfo['out_over_charge'];//超公里
		}else{
			$flag_fall_price = $cartypeInfo['jp_price'];//起步价
			$over_charge = $cartypeInfo['jp_over_charge'];//超公里
		}
		if($data['mile']<=10)
            $car_price=$flag_fall_price;
        else
            $car_price=$flag_fall_price+($data['mile']-10)*$over_charge;

		$arr['status']=$data['status'];
		$arr['cartype_info']=json_encode([
            'cartype_id'		=>$cartypeInfo['id'],
            'cartype_title'		=>$cartypeInfo['title'],
            'flag_fall_price'	=>$cartypeInfo['flag_fall_price'],
            'over_charge'		=>$cartypeInfo['over_charge'],
            'mile'				=>$data['mile'],
            'price'				=>$car_price
         ]);
		$arr['product_info']=json_encode([
            'product_id'		=>$data['product_id'],
            'product_title'		=>$productInfo['title'],
            'floor_from'		=>$data['floor_from'],
            'floor_to'			=>$data['floor_to'],
            'floor_from_cost'	=>$data['floor_from_cost'],
            'floor_to_cost'		=>$data['floor_to_cost'],
            'price'				=>$data['floor_from_cost']+$data['floor_to_cost']
        ]);
        $arr['order_no_small'] 	= $data['order_no_small'];
        $arr['order_no'] 		= $data['order_no'];
        $arr['addtime'] 		= $data['addtime'];
        $arr['booktime'] 		= $data['booktime'];
        $arr['total_price'] 	= $data['total_price'];
        $arr['advance_price'] 	= $data['advance_price'];
        $arr['extra_price'] 	= $data['extra_price'];
        $arr['arrival'] 		= $data['arrival'];
        $arr['departure'] 		= $data['departure'];
        $arr['tel'] 			= $data['tel'];
        $arr['small_balance'] 	= $data['small_balance'];
        $arr['move_price'] 		= $data['move_price'];
        $arr['product_id'] 		= $data['product_id'];
        $arr['member_id'] 		= $data['member_id'];
        $arr['mile'] 			= $data['mile'];
        // ll($arr);die;
        $res = M('order')->add($arr);
        if($res){
        	$del = M('order_valuation')->where(['id'=>$data['id']])->delete();
     		$this->selectPage_ajax( array('act' => '帮助下单操作', 'back_url' =>U('Buyers/index'),'jump_url' => U('Buyers/index')));
        }else{
        	$this->selectPage_ajax(['act'=>'操作未']);
        }
	}
	public function driver_save(){
		$temp = $this->m->create();
		$isset = $this->m->where(['username'=>$temp['username']])->find();
		if($isset){
			$this->selectPage_ajax(['act'=>'账号已存在重新输入，操作未']);
		}
		$temp['pwd'] = md5($tempp['pwd']);
		$result = $this->m->add($temp);
		if($result){
		    $this->selectPage_ajax( array('act' => '添加', 'back_url' =>U('Driver/index','id='.$temp['id']),'jump_url' => U('Driver/index','id='.$temp['id'])));
		}else{
			$this->selectPage_ajax(['act'=>'操作未']);
		}
	}
	public function driver_edit(){
		$this->display('driver_edit');
	}

	public function del(){
		$ids = I('id');
		$this->m->where(['id'=>$ids])->delete();
		$this->success($GLOBALS['notice']['success'][2]);
	 	// $this->selectPage_ajax( array('act' => '操作成功', 'back_url' =>U('Buyers/index'),'jump_url' => U('Buyers/index')));
    }

	public function batch(){          ////批量操作方法



		

		$ids = I('id');

		$val = I('get.val');

		$field = I('get.field');

		if($ids){

			if(is_array($ids)){$ids=implode(",",$ids);}

			

			if ($this->m->where('id in ('.$ids.')')->save(array($field=>$val))){

				$ids=explode(",",$ids);

			    $this->success($GLOBALS['notice']['success'][2]);

			}else{

				$this->error($GLOBALS['notice']['error'][2]);

			}

		}else{

			$this->error($GLOBALS['notice']['warming'][1]);

		}		

    }

	public function ajaxFindNext(){



		$selValue = I('selValue','0','int');

		$areaNextId = I('areaNextId','','int');

		$domId = I('domId');

		$classList = $selValue?$this->m_class->where('parent_id='.$selValue)->select():array();

		$content = "";

		if(!count($classList)){

			echo 0;exit;

		}else{

		

			$content .= '<span id="'.$domId.'_'.$areaNextId.'">'."\n\t";

			$content .= '<select class="form-control input-small" name="class_id[]" onchange="javascript:findNext(this.value , '.$areaNextId.' , \''.$domId.'\')">'."\n\t";

			$content .= '<option value="0">请选择</option>'."\n\t";

			foreach($classList as $item){

			$selectedMark = $selValue==$item['id']?'selected':'';

			$content .= '<option value="'.$item['id'].'" '.$selectedMark.'>'.$item['class_name'].'</option>'."\n\t";

			}

			$content .= '</select>'."\n\t";

			$content .= '</span>'."\n\t";

		

			echo $content;exit;

		

		}

	}

	public function ajaxListInput(){

		header('Content-Type:text/html;charset=utf-8');

		$info = I('post.');

		$data[$info["fieldName"]] = $info["Val"];

		$data["id"] = $info["id"];

		if ($this->m->save($data)){

			echo "success";

		}else{

			echo "error";

		}

		exit;

	}

	

	

	

	

}